package com.net.tomcat5.Servlet3;
import java.util.Map;

/**
 * 通过反射进行
 *
 * @author jiangbang
 * @date 2016/8/25
 */
public class WebApp {
    private static ServletContext context;
    static{
        context = new ServletContext();
        Map<String,String> mapping = context.getMapping();
        mapping.put("/login","login");
        mapping.put("/log","log");
        mapping.put("/reg","register");

        Map<String,String> servlet = context.getServlet();
        servlet.put("login","com.net.tomcat5.Servlet3.LoginServlet");
        servlet.put("register","com.net.tomcat5.Servlet3.RigisterServlet");
    }

    public static Servlet getServlet(String url) {
        if(null==url || url.equals("")){
            return  null;
        }
        //根据字符创创建对象
        String name =  context.getServlet().get(context.getMapping().get(url));
        try {
            return (Servlet)Class.forName(name).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


}
